﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNet.Utilities
{
    public class TimeHelper
    {
        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name=”timeStamp”></param>
        /// <returns></returns>
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name=”time”></param>
        /// <returns></returns>
        public static string ConvertDateTime(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return Convert.ToString((int)(time - startTime).TotalSeconds);
        }

        /// <summary>
        /// 判断传入的日期是否是当天或之后;
        /// </summary>
        /// <param name="compareDayStr">要比较的日期字符串（如2015-05-06）</param>
        /// <returns>是否</returns>
        public static bool TodayOrLater(string compareDayStr)
        {
            string todayStr = DateTime.Now.ToString("yyyy-MM-dd");
            DateTime today = Convert.ToDateTime(todayStr);
            DateTime compareDay = Convert.ToDateTime(compareDayStr);

            int compare = DateTime.Compare(compareDay, today);
            if (compare >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
